package com.hosp.service;

import java.util.List;
import java.util.Objects;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hosp.bean.Sysmenu;
import com.hosp.bean.SysmenuExample;
import com.hosp.mapper.SysmenuMapper;
import com.hosp.vo.PageRequestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author ztranscript 2021/9/12 21:35
 **/
@Service
public class SysResourceService {
    @Autowired
    private SysmenuMapper sysmenuMapper;

    /**
     * 保存系统资源
     *
     * @param sysmenu 保存对象
     * @return 响应
     */
    public boolean save(Sysmenu sysmenu) {
        int row = 0;
        if (Objects.isNull(sysmenu.getmId())) {
            // 新增
            row = sysmenuMapper.insert(sysmenu);
        } else {
            // 修改
            row = sysmenuMapper.updateByPrimaryKey(sysmenu);
        }
        return row > 0;
    }

    /**
     * 查询列表
     *
     * @param sysmenu     查询条件
     * @param pageRequest 分页条件
     * @return 响应
     */
    public PageInfo<Sysmenu> list(Sysmenu sysmenu, PageRequestVO pageRequest) {
        SysmenuExample example = new SysmenuExample();
        SysmenuExample.Criteria criteria = example.createCriteria();
        String name = sysmenu.getmName();
        if (!StringUtils.isEmpty(name)) {
            criteria.andMNameLike("%" + name + "%");
        }
        // 分页查询
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        List<Sysmenu> sysmenus = sysmenuMapper.selectByExample(example);
        return new PageInfo<>(sysmenus);
    }

    /**
     * 删除系统资源
     *
     * @param mIds 待删除 IDs
     * @return 响应
     */
    public boolean delete(List<Integer> mIds) {
        int row = 0;
        for (Integer mId : mIds) {
            sysmenuMapper.deleteByPrimaryKey(mId);
            row++;
        }
        return row == mIds.size();
    }

    /**
     * 查询列表
     *
     * @param mId 查询条件
     * @return 响应
     */
    public Sysmenu selectById(Integer mId) {
        return sysmenuMapper.selectByPrimaryKey(mId);
    }

    public boolean check(String mName) {
        SysmenuExample example = new SysmenuExample();
        SysmenuExample.Criteria criteria = example.createCriteria();
        criteria.andMNameEqualTo(mName);
        List<Sysmenu> list = sysmenuMapper.selectByExample(example);
        return list.size() > 0;
    }
}
